// 获取文章总数
function showAllArticlesCount(menuId,tagId,title){
	var datas={
		'menuId':menuId,
		'tagId':tagId,
		'title':title
	};
	$.ajax({
		url:'article/getArticlesCount.do',
		data:datas,
		type:'post',
		dataType:'json',
		success: function(result){
			if(result.state==0){
				var totalCount=result.data;
				showArticleList(totalCount,menuId,tagId,title);
			}
		},
		error: function(e){
			alert("通信失败!");
		}
	});
}
function showArticleList(totalCount,menuId,tagId,title){
	$("#center").html("");
	if(totalCount==0){
		$("#center").append('<div class="no_articles"><h1>There is no articles here!<h1></div>');
		return;
	}else{
		$("#center").append('<div id="articleZoom"></div>');
		$("#center").append('<div style="width:100%;" id="page"><div class="box" '
				+' style="float:right;width:60%;margin:20px 0px 0px 0px;" id="box"></div></div>');
		pageDiv(totalCount,menuId,tagId,title);
	}
}

//分页
function pageDiv(totalCount,menuId,tagId,title){
	var pageSize=10;
	var totalPages = Math.ceil(totalCount/pageSize);
	$('#box').paging({
		initPageNo: 1, // 初始页码
		totalPages: totalPages, // 总页数
		totalCount: '合计' + totalCount + '条数据', // 条目总数
		slideSpeed: 600, // 缓动速度。单位毫秒
		jump: true, // 是否支持跳转
		callback: function(page) { // 回调函数
			showAllArticles(menuId,tagId,title,page,pageSize)
		}
	})
}


//获取文章数据
function showAllArticles(menuId,tagId,title,pageNum,pageSize){
	var datas={
		'menuId':menuId,
		'tagId':tagId,
		'title':title,
		'page':pageNum,
		'pageSize':pageSize
	};
	$.ajax({
		url:'article/getArticles.do',
		data:datas,
		type:'post',
		dataType:'json',
		success: function(result){
			console.log(result);
			if(result.state==0){
				var articles=result.data;
				showArticleListMsg(articles);
			}
		},
		error: function(e){
			alert("通信失败!");
		}
	});
}

function showArticleListMsg(articles){
	$("#articleZoom").html("");
	var len = articles.length;
	for(var i=0;i<len;i++){
		var article = articles[i];
		
		var title = article.title;
		var id = article.id;
		var visitNum = article.visitNum;
		var authorId = article.author;
		var user = getUserDetails(authorId);
		
		var tagIds = article.tagId;
		var lastModifyTime = article.lastModifyTime;
		lastModifyTime = new Date(parseFloat(lastModifyTime)).format("yyyy-MM-dd hh:mm:ss");
		
		var tagA="";
		if(tagIds!=null||tagIds.length!=0||tagIds!=undefined){
			var tagLists = tagIds.split(",");
			tagLists.forEach(function(value){
				tagA += '<a class="post-tag">'+value+'</a>';
			})
		}
		// 获取评论数量
		var commentNum = statistCommentsNum(id);
		
		var divList='<div class="question-summary narrow">'+
						'<div class="cp">'+
						    '<div class="status answered">'+
						        '<div class="mini-counts"><span title="">'+commentNum+'</span></div>'+
						        '<div>comments</div>'+
						    '</div>'+
						    '<div class="views">'+
						        '<div class="mini-counts hot"><span title="">'+visitNum+'</span></div>'+
						        '<div class="hot">views</div>'+
						    '</div>'+
						'</div>'+
						'<div class="summary">'+
						    '<h2><a class="title" id='+id+'>'+title+'</a></h2>'+
						    '<div class="tags">'+tagA+'</div>'+
						    '<div class="started">'+
							    '<a>'+lastModifyTime+'</a>&nbsp;'+
							    '<a href="">'+user.nick+'</a>'+
						    '</div>'+
						'</div>'+
					'</div>';
		
		$("#articleZoom").append(divList);
	}
}

//统计文章的评论数量
function statistCommentsNum(articleId){
	var commentNum='';
	var datas={'articleId':articleId};
	$.ajax({
		url:'article/statistCommentsNum.do',
		data:datas,
		async:false,
		type:'post',
		dataType:'json',
		success: function(result){
			if(result.state==0){
				commentNum=result.data;
			}else{
				alert(result.message);
			}
		},
		error: function(e){
			alert("通信失败!");
		}
	});
	return commentNum;
}

function farmatDate(){
	Date.prototype.format = function(format) {
        var o = {
            "M+" : this.getMonth() + 1,// month
            "d+" : this.getDate(),// day
            "h+" : this.getHours(),// hour
            "m+" : this.getMinutes(),// minute
            "s+" : this.getSeconds(),// second
            "q+" : Math.floor((this.getMonth() + 3) / 3),// quarter
            "S" : this.getMilliseconds()
        // millisecond
        };
        if (/(y+)/.test(format) || /(Y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for ( var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    };
	
}



















